
import 'dart:collection';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:verse_systems/commom/models/application_model.dart';
import 'package:verse_systems/commom/models/apply_model.dart';
import 'package:verse_systems/commom/models/loan_model.dart';
import 'package:verse_systems/commom/routers/navigator_utils.dart';
import 'package:verse_systems/commom/routers/routes_pages_name.dart';
import 'package:verse_systems/pages/home/home_control.dart';
import 'package:verse_systems/pages/mine/mine_control.dart';
import 'package:verse_systems/pages/orderdetails/order_details_state.dart';
import 'package:verse_systems/net/http_dio.dart';
import 'package:verse_systems/utils/account_util.dart';


class OderDetailsControl extends GetxController {
  final OderDetailsState state = OderDetailsState();
  
   // ignore: non_constant_identifier_names
   final home_control = Get.find<HomeControl>();

 getOrderDetails() async {
    final String token = await AccountUtil.getUserToken();
    final borrowId = Get.arguments["borrow_id"];

    Request.postCallback(
      HttpUrls.getOrderDetails,
      data: {'token': token,'borrow_id':borrowId},
      success: (res) async {
        ResultData result = ResultData.fromJson(res.data);
         debugPrint("订单详情 ----->$result");
        if (result.status == 0) {
          final Map<String, dynamic> application =   result.data["data"]["application"];
          final Map<String, dynamic> apply =   result.data["data"]["apply"];
          final Map<String, dynamic> loan =   result.data["data"]["loan"];
        
          state.orderType  = result.data["type"];
          state.notice  = result.data["notice"];
          state.application = ApplicationModel.fromJson(application);
          state.apply = ApplyModel.fromJson(apply);
          state.loan = LoanModel.fromJson(loan);
          update();
        }
      },
    );
  }

 onApplication(){//certify 是否进行认证,true认证,false进入额度确认
    final borrowId = Get.arguments["borrow_id"];
   if (state.application!.certify) {
        home_control.nextCertification(borrowId: borrowId.toString());
   } else {
      Get.toNamed(RoutesPage.Certification,arguments: {"borrow_id": borrowId});    
   } 
 } 

 
 //extend
   repayment ({String type = "repay" }) async {
      debugPrint("还款");
    final borrowId = Get.arguments["borrow_id"];
    final String token = await AccountUtil.getUserToken();
    Map<String, dynamic> map = HashMap();
    map['token'] = token;
    map['borrow_id'] = borrowId;
    map['type'] = type;
    Request.postCallback(
      HttpUrls.repaymentOrder,
      data: map,
      success: (res) async {
        ResultData result = ResultData.fromJson(res.data);
        debugPrint("repaymentOrder--$result");
        if (result.status == 0) {
            final apply = result.data["apply"];
            if (apply) {
                final borrowId = Get.arguments["borrow_id"];
                Map<String, dynamic> map = HashMap();
                map['type'] = type;
                map['borrow_id'] = borrowId;
                Get.toNamed(RoutesPage.RepaymentonBankInfo,arguments: map); 
            } else {
                if (type == "repay") {
                    Get.toNamed(RoutesPage.RepaymentonCfirm,arguments: {"borrow_id":borrowId,"type":"repay"});
                } else {
                    Get.toNamed(RoutesPage.RepaymentonExtend,arguments: {"borrow_id":borrowId,"type":"extend"}); 
                }
               
            }
        }
      },
    );      
  }
  
   rollover () {
      debugPrint("展期");
      repayment(type: "extend");
    }


  onPressed(){
      final product = (Get.arguments == null)?false: Get.arguments["product"];
      if (product == true ) {
          NavigatorUtils.goToHome();
          Get.put(MineControl());
          Get.put(HomeControl());
          Get.toNamed(RoutesPage.OrderPage);
      } else {
        Get.back();
      } 
    }

  }